﻿using System;
//
using SKYPE4COMLib;

// SkypeScreenSaver, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace SkypeScreenSaver
{
    class CWakeUpSkype : IWakeUp
    {
        private Skype S = null;
        //
        public event WakeUpHandler eventWakeUp;
        //==============================================================
        public CWakeUpSkype()
        {
            ;
        }
        //==============================================================
        public bool Start()
        {
            try
            {
                S = new Skype();
                //
                S.Attach(5, false);
                //
                if (S.Client.IsRunning)
                {
                    SL.Log.Write("CWakeUpSkype.Start");
                    //S.Client.Start(true, true);
                    S.CallStatus += new _ISkypeEvents_CallStatusEventHandler(Skype_CallStatus);
                }
                else
                {
                    SL.Log.Write("! CWakeUpSkype.Start");
                    S = null; // jak nie działa, to nie odbierze połaczenia
                }
                //
            }
            catch(Exception e)
            {
                SL.Log.FWrite("CWakeUpSkype.Start catch:{0}", e.Message);
                S = null;
                return false;
            }
            return true;
        }
        //==============================================================
        public bool Stop()
        {
            //
            try
            {
                if (null != S)
                {
                    S.CallStatus -= new _ISkypeEvents_CallStatusEventHandler(Skype_CallStatus);
                    S = null;
                }
                SL.Log.FWrite("CWakeUpSkype.Stop");
            }
            catch(Exception e)
            {
                SL.Log.FWrite("CWakeUpSkype.Stop catch:{0}", e.Message);
                return false;
            }
            //
            return true;
        }
        //==============================================================
        public string Info()
        {
            return "Skype: " + S != null ? S.Version : "?";
        }
        //==============================================================
        //
        private void Skype_CallStatus(SKYPE4COMLib.Call aCall, TCallStatus aStatus)
        {
            if (TCallStatus.clsRinging == aStatus)
            {
                SL.Log.FWrite("CWakeUpSkype.TCallStatus.clsRinging");
                //
                if (null != eventWakeUp)
                {
                    eventWakeUp.Invoke();
                }
                //
            }
        }
        //
        //==============================================================

    }
}
